<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入会员昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员性别" prop="sex">
        <el-select v-model="queryParams.isEnable" placeholder="请选择会员性别" clearable>
          <el-option
            v-for="dict in dict.type.sys_user_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="isEnable">
        <el-select v-model="queryParams.sex" placeholder="请选择是否有效" clearable>
          <el-option
            v-for="dict in dict.type.sys_is_enable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="会员等级" prop="grade">
        <el-select v-model="queryParams.grade" placeholder="请选择会员等级" clearable>
          <el-option
            v-for="dict in gradeList"
            :key="dict.gradeLevel"
            :label="dict.gradeName"
            :value="dict.gradeLevel"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['uac:member:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['uac:member:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['uac:member:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['uac:member:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="会员头像" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-image style="width: 60px;border-radius: 12px" fit="contain" :src="scope.row.avatar"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userName"/>
      <el-table-column label="会员昵称" align="center" prop="nickName"/>
      <el-table-column label="手机号码" align="center" prop="mobile"/>
      <el-table-column label="会员等级" align="center" prop="grade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="会员性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="会员生日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center" prop="isEnable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.isEnable"/>
        </template>
      </el-table-column>
      <el-table-column label="客户端" align="center" prop="clientEnum"/>
      <el-table-column label="最后一次登录时间" align="center" prop="lastLoginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLoginDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">


          <el-dropdown>
            <el-button type="primary" size="small">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-hasPermi="['uac:member:edit']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['uac:member:remove']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['uac:member:remove']">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-refresh-right"
                  @click="handleResetPwd(scope.row)"
                >重置密码
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="handleMemberInfo(scope.row)"
                >详情
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </template>

      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员用户名" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入会员用户名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="!form.id">
            <el-form-item label="会员密码" prop="password">
              <el-input v-model="form.password" placeholder="请输入会员密码"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="会员昵称" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请输入会员昵称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="mobile">
              <el-input v-model="form.mobile" placeholder="请输入手机号码"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="会员性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择会员性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否有效" prop="isEnable">
              <el-select v-model="form.isEnable" placeholder="请选择是否有效">
                <el-option
                  v-for="dict in dict.type.sys_is_enable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="会员邮箱" prop="email">
              <el-input v-model="form.email" placeholder="请输入会员邮箱"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员生日" prop="birthday">
              <el-date-picker clearable
                              v-model="form.birthday"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择会员生日">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" rows="3" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 重置密码对话框 -->
    <el-dialog title="重置密码" :visible.sync="resetPwdOpen" v-if="resetPwdOpen" width="50%" append-to-body>
      <el-form ref="resetPwdForm" :model="resetPwdForm" :rules="resetPwdRules" label-width="100px">
        <el-form-item label="新密码" prop="password">
          <el-input v-model="resetPwdForm.password" placeholder="请输入新密码(密码的长度必须在6到20位之间，且只能包含字母、数字和特殊字符中的一种或多种组合)"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitResetPwdForm">确 定</el-button>
        <el-button @click="resetPwdOpen == false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 会员详情对话框 -->
    <el-dialog title="会员详情" :visible.sync="memberInfoOpen" v-if="memberInfoOpen" width="90%" append-to-body>
     <member-info-router :memberId="memberId"></member-info-router>
      <div slot="footer" class="dialog-footer">
        <el-button @click="memberInfoOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMember, getMember, delMember, addMember, updateMember, resetMemberPwd} from "@/api/uac/member";
import {listGradeAll} from "@/api/uac/grade";
import MemberInfoRouter from '@/views/uac/member/member-info-router'

export default {
  name: "Member",
  dicts: ['sys_user_sex', 'sys_is_enable'],
  components: {
    MemberInfoRouter
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      memberList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展示重置密码弹出层
      resetPwdOpen: false,
      // 会员详情弹出层
      memberInfoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        nickName: null,
        password: null,
        mobile: null,
        email: null,
        avatar: null,
        sex: null,
        grade: null,
        birthday: null,
        isEnable: null,
        clientEnum: null,
        lastLoginDate: null,
        wxMpOpenid: null,
        wxXcxOpenid: null,
      },
      // 表单参数
      form: {},
      resetPwdForm: {
        id: null,
        password: null
      },
      // 表单校验
      rules: {
        sex: [
          {required: true, message: "会员性别不能为空", trigger: "change"}
        ],
      },
      // 重置密码表单校验
      resetPwdRules: {
        password: [
          {required: true, message: "密码不能为空", trigger: "change"},
          { min: 6, max: 20, message: '用户密码长度必须介于 6 和 20 之间', trigger: 'blur' },
          {
            pattern: /^[A-Za-z0-9?!@#$%^&*]{6,20}$/,
            message: "密码的长度必须在6到20位之间，且只能包含字母、数字和特殊字符中的一种或多种组合",
            trigger: "blur"
          }
        ],
      },
      gradeList: [],
      memberId: null
    };
  },
  created() {
    this._listGradeAll();
    this.getList();
  },
  methods: {
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /* 加载全部会员等级 */
    _listGradeAll() {
      listGradeAll().then(response => {
        this.gradeList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        nickName: null,
        password: null,
        mobile: null,
        avatar: null,
        sex: null,
        grade: null,
        birthday: null,
        isEnable: null,
        clientEnum: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMember(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员";
      });
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      const id = row.id || this.ids
      this.resetPwdForm.id = row.id;
      this.resetPwdOpen = true;
    },
    /** 会员详情按钮操作 */
    handleMemberInfo(row) {
      this.memberId = row.id;
      this.memberInfoOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 重置密码提交按钮 */
    submitResetPwdForm() {
      this.$refs["resetPwdForm"].validate(valid => {
        if (valid) {
          resetMemberPwd(this.resetPwdForm).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.resetPwdOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员编号为"' + ids + '"的数据项？').then(function () {
        return delMember(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('uac/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
